import * as types from '../actionTypes'

const initState = {
  tagList: [{ title: '公告板', path: '/dashboard' }]
}
export default function tagsView(state = initState, action) {
  switch (action.type) {
    case types.TAGSVIEW_ADD_TAG:
      const menuList = action.currentMenuList
      const tag = menuList.length === 1 ? menuList[0] : menuList[1]

      const hasTag = state.tagList.some(item => {
        return item.path === tag.path
      })

      if (hasTag) {
        return state
      } else {
        return {
          ...state,
          tagList: [...state.tagList, tag]
        }
      }
    case types.TAGSVIEW_DELETE_TAG:
      const newTag = state.tagList.filter(item => item !== action.tag)

      return {
        ...state,
        tagList: [
          ...newTag
        ]
      }
    case types.TAGSVIEW_EMPTY_TAGLIST:
      return {
        ...state,
        tagList:  [
          ...state.tagList.filter(item => item.path === '/dashboard')
        ]
      }
    case types.TAGSVIEW_CLOSE_OTHER_TAGS:
      return {
        ...state,
        tagList:  [
          ...state.tagList.filter(item => item.path === '/dashboard' || item === action.tag)
        ]
      }
    default:
      return state
  }
}
